package service;

import dao.UserDao;
import entity.PageEntity;
import entity.User;

import java.util.List;

public class UserService {
    private UserDao userDao = new UserDao();

    /**
     * 根据用户名查询用户
     *
     * @param username
     * @return
     */
    public User fingByUsername(String username) {
        return userDao.fingByUsername(username);
    }

    public User fingByEmail(String email) {
        return userDao.fingByEmail(email);
    }


    public void register(User user) {
        userDao.register(user);
    }


    public List<User> findAll() {

        return userDao.findAll();
    }

    public List<User> search(String searchVal) {
        return userDao.search(searchVal);
    }

    /**
     * 查询分页信息
     *
     * @param username
     * @param currPage
     * @return
     */
    public PageEntity<User> findPage(String username, Integer currPage) {

        PageEntity<User> pageData = new PageEntity<>();

        //页大小
        Integer pageSize = pageData.getPageSize();

        //1.当前页的数据
        Integer startIndex = (currPage - 1) * pageSize;
        List<User> rowData = userDao.findPage(username, startIndex, pageSize);

        //2.总记录数
        Integer totalCount = userDao.countByUsername(username);

        //3.总页数
        //总页数 = （总记录数 % 页大小 ==0？ 总记录数 /页大小 ： 总记录数 / 页大小+1）

        Integer totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;

        pageData.setCurrPage(currPage);
        pageData.setData(rowData);
        pageData.setTotalCount(totalCount);
        pageData.setTotalPage(totalPage);

        return pageData;
    }

    /**
     * 根据部门id查询员工
     * @param deptId
     * @return
     */
    public List<User> findByDeptId(Long deptId) {
        return userDao.findByDeptId(deptId);
    }

    /**
     * 查询用户详情
     * @param userId
     * @return
     */
    public User findById(long userId) {
        return userDao.findById(userId);
    }

    /**
     * 查询用户粉丝数
     * @param userId
     */
    public Integer findFansCount(long userId) {
        return userDao.findFansCount(userId);

    }

    /**
     * 被看数+1
     * @param userId
     */
    public void updateLook(long userId) {
         userDao.updateLook(userId);

    }

    /**
     * 查询用户关注数
     * @param userId
     * @return
     */
    public Integer findFocusCount(Long userId) {
        return userDao.findFocusCount(userId);
    }

    public void update(User loginUser) {
         userDao.update(loginUser);
    }

    public void updatePassword(User user) {
        userDao.updatePassword(user);
    }

    /**
     * 更新地址头像
     * @param id
     * @param url
     */
    public void updatePic(Long id, String url) {

        userDao.updatePic(id,url);
    }

    /**
     * 根据id查询这个用户关注的用户列表
     * @param id
     * @return
     */
    public List<Long> findFocusIdList(Long id) {
       return userDao.findFocusIdList(id);
    }

    /**
     * 是否关注过对方
     * @param userId
     * @param focusId
     * @return
     */
    public Boolean isFocus(Long userId, Long focusId) {

        return userDao.isFocus(userId,focusId);
    }

    /**
     * 取消关注
     * @param userId
     * @param focusId
     */
    public void delFocus(Long userId, Long focusId) {
         userDao.delFocus(userId,focusId);
    }

    /**
     * 加关注
     * @param userId
     * @param focusId
     */
    public void addFocus(Long userId, Long focusId) {
          userDao.addFocus(userId,focusId);
    }

    /**
     * 查询分页信息
     * @param currPage
     * @param userId
     * @return
     */
    public PageEntity<User> findFocusPage(Integer currPage, Long userId) {
        PageEntity<User> pageData = new PageEntity<>();
        Integer pageSize = pageData.getPageSize();

        //当前页的数据
        Integer startIndex = (currPage-1)* pageSize;
        List<User> rowData = userDao.findFocusPage(userId,startIndex,pageSize);

        //总记录数
        Integer totalCount = userDao.countFocusByUserId(userId);

        //总页数
        Integer totalPage = (totalCount % pageSize == 0) ? totalCount / pageSize : totalCount / pageSize + 1;

        pageData.setCurrPage(currPage);
        pageData.setData(rowData);
        pageData.setTotalCount(totalCount);
        pageData.setTotalPage(totalPage);

        return pageData;
    }

    /**
     *更改用户登录时间
     * @param userId
     */
    public void updateLoginTime(Long userId) {
        userDao.updateLoginTime(userId);

    }

    /**
     * 根据微信的openId查询用户
     * @param openid
     * @return
     */
    public User findByWxOpenId(String openid) {
        return userDao.findByWxOpenId(openid);

    }
}
